require 'View/UIBase/UIBase'

UIBattlePassTips = UIBase.New(PanelResNames.UIBattlePassTips)

function UIBattlePassTips.Init(func, id, type)
    UIBattlePassTips.activeId = id;
    UIBattlePassTips.type = type;
    UIBase.Init(UIBattlePassTips, func);
end

function UIBattlePassTips:OnCreate()
    self.drawWrapper = GoWrapper.New()
    self.lihui = self:GetChild("lihui");
    self.lihui2 = self:GetChild("lihui2");
    self.buyTips = self:GetChild("buyTips")
    self.title = self:GetChild("title")
    self.buyAllItemList = self:GetChild("buyAllItemList")
    self.buyNowItemList = self:GetChild("buyNowItemList")
    self.buyBtn = self:GetChild("buy")
    self.buyBPList = self:GetChild("buyBPList");
    self.bpNum = self:GetChild("BPNumTips");
    self.bpBuyNum = self:GetChild("bpBuyNum");
    self.bpMoney = self:GetChild("bpMoney");
    self.buyBPBtn = self:GetChild("buyBPBtn")
    self.buyBPBtn.title = getLanguage("OptionalPool_Btn_Buy")
    self.sure = self:GetChild("sure")
    self.sure.title = getLanguage("Btn_Confirm")

    self.tipsController = self:GetController("tips")


    self:RegisterOnClick(self:GetChild("buy"),self.OnBuy);
    self:RegisterOnClick(self:GetChild("buyBPBtn"),self.OnBuyBP);
    self:RegisterOnClick(self:GetChild("sure"),self.OnSure);
    self:RegisterOnClick(self:GetChild("bpBuyAdd"),self.ClickBpBuyAdd);
    self:RegisterOnClick(self:GetChild("bpBuyMinus"),self.ClickBpBuyMinus);
    self:RegisterOnClick(self:GetChild("bpBuyMax"),self.ClickBpBuyMax);
    self:RegisterOnClick(self:GetChild("closeBtn"),self.OnClose);
end

-- 显示
function UIBattlePassTips:OnEnable()
    if self.activeId == nil then
        self:OnBack();
    else
        self:Refresh();
    end
end

function UIBattlePassTips:Refresh()
    self.severData, self.itemTableData, self.modelTableData = logicMgr.CostManager.getCostData(self.activeId);
    self.title.text = getLanguage(self.modelTableData.f_Title);
    self.buyBtn.title = "￥"..self.modelTableData.f_Price;
    self.buyTips.text = getLanguage(self.modelTableData.f_BuyTips);
    self.chooseItemIndex = nil;
    if self.type == 2 then
        self:ShowBuyBP();
    elseif self.type == 3 then
        self:ClickChoose();
    else
        self:ShowBuy();
    end
    logicMgr.CostManager.updateLiHui(self, self.drawWrapper, self.lihui, self.lihui2, self.modelTableData.f_HeroSpine);
end

function UIBattlePassTips:ShowBuy()
    self.tipsController.selectedIndex = 0;
    local allindex = 1;
    local nowindex = 1;
    local allReward = {};
    local nowReward = {};
    local allIndexs = {};
    local nowIndexs = {};
    local reward;
    for k, v in pairs(self.severData.items) do
        local tableData = self.itemTableData[tostring(v.subId)];
        if tableData ~= nil then
            reward = decodeJsonStrToTabel(tableData.f_AdvancedReward);
            for key, value in pairs(reward) do
                if allIndexs[value[1]] == nil then
                    allIndexs[value[1]] = allindex;
                    table.insert(allReward, {value[1], 0});
                    allindex = allindex + 1;
                end
                allReward[allIndexs[value[1]]][2] = allReward[allIndexs[value[1]]][2] + value[2];
                if self.severData.progress >= tableData.f_NeedPoint then
                    if nowIndexs[value[1]] == nil then
                        nowIndexs[value[1]] = nowindex;
                        table.insert(nowReward, {value[1], 0});
                        nowindex = nowindex + 1;
                    end
                    nowReward[nowIndexs[value[1]]][2] = nowReward[nowIndexs[value[1]]][2] + value[2];
                end
            end
        end
    end
    logicMgr.CostManager.RefreshItemList(self.buyAllItemList, allReward, 3);
    logicMgr.CostManager.RefreshItemList(self.buyNowItemList, nowReward, 3);
end

function UIBattlePassTips:ShowBuyBP()
    self.tipsController.selectedIndex = 1;
    self.bpProportion = decodeJsonStrToTabel(self.modelTableData.f_PointPrice);
    local bpIndex = self.severData.needBPIndex;
    local tableData = self.itemTableData[tostring(self.severData.items[bpIndex].subId)];
    self.needBpPart = math.floor((tableData.f_NeedPoint - self.severData.progress + self.bpProportion[2] - 1) / self.bpProportion[2]);
    self.needBpIndex = bpIndex + 1;
    self:updateBpNum();
    self:updateBpList();
end

function UIBattlePassTips:ClickBpBuyAdd()
    if self.needBpIndex > #self.severData.items then
        UISysTips.AddMsg("不能再多了哦");
        return ;
    end
    self.needBpPart = self.needBpPart + 1;
    self:updateBpNum();
    local tableData = self.itemTableData[tostring(self.severData.items[self.needBpIndex].subId)];
    if self.severData.progress + self.needBpPart * self.bpProportion[2] >= tableData.f_NeedPoint then
        self.needBpIndex = self.needBpIndex + 1;
    end
    self:updateBpList();
end

function UIBattlePassTips:ClickBpBuyMinus()
    if self.needBpPart <= 1 then
        UISysTips.AddMsg("不能再少了哦");
        return ;
    end
    self.needBpPart = self.needBpPart - 1;
    self:updateBpNum();
    local tableData = self.itemTableData[tostring(self.severData.items[self.needBpIndex - 1].subId)];
    if self.severData.progress + self.needBpPart * self.bpProportion[2] < tableData.f_NeedPoint then
        self.needBpIndex = self.needBpIndex - 1;
    end
    self:updateBpList();
end

function UIBattlePassTips:ClickBpBuyMax()
    local bpIndex = #self.severData.items;
    local tableData = self.itemTableData[tostring(self.severData.items[bpIndex].subId)];
    self.needBpPart = math.floor((tableData.f_NeedPoint - self.severData.progress + self.bpProportion[2] - 1) / self.bpProportion[2]);
    self.needBpIndex = bpIndex + 1;
    self:updateBpNum();
    local tableData = self.itemTableData[tostring(self.severData.items[self.needBpIndex - 1].subId)];
    if self.severData.progress + self.needBpPart * self.bpProportion[2] < tableData.f_NeedPoint then
        self.needBpIndex = self.needBpIndex - 1;
    end
    self:updateBpList();
end

function UIBattlePassTips:updateBpNum()
    self.bpNum.text = string.format(getLanguage(self.modelTableData.f_BuyNumTips),self.needBpPart * self.bpProportion[2]);
    self.bpBuyNum.text = self.needBpPart * self.bpProportion[2];
    self.bpMoney.text = self.needBpPart * self.bpProportion[1];
end

function UIBattlePassTips:updateBpList()
    local data = {};
    local keyData = {};
    for i = self.severData.needBPIndex, self.needBpIndex - 1 do
        local tableData = self.itemTableData[tostring(self.severData.items[i].subId)];
        for k, v in pairs(decodeJsonStrToTabel(tableData.f_BaseReward)) do
            if keyData[v[1]] == nil then
                table.insert(data, {v[1], 0});
                keyData[v[1]] = #data;
            end
            data[keyData[v[1]]][2] = data[keyData[v[1]]][2] + v[2];
        end
        if self.severData.buyCount > 0 then
            for k, v in pairs(decodeJsonStrToTabel(tableData.f_AdvancedReward)) do
                if keyData[v[1]] == nil then
                    table.insert(data, {v[1], 0});
                    keyData[v[1]] = #data;
                end
                data[keyData[v[1]]][2] = data[keyData[v[1]]][2] + v[2];
            end
        end
    end
    logicMgr.CostManager.RefreshItemList(self.buyBPList, data, 3);
    --self.buyBPList:ScrollToView(#data - 1);
end

function UIBattlePassTips:ClickChoose()
    self.tipsController.selectedIndex = 2;
    local candidateList = self:GetChild("chooseList");
    local icon = self:GetChild("chooseItems");
    icon:GetController("choose").selectedIndex = 1;
    local tableData = decodeJsonStrToTabel(self.modelTableData.f_ShowType);
    candidateList:RemoveChildrenToPool();
    for k, v in pairs(tableData) do
        local item = candidateList:AddItemFromPool();
        logicMgr.CostManager.RefreshItem(item, {v[2]});
        item.onClick:Clear();
        item.onClick:Add(function() self:ClickOneChoose(v); end);
    end
    if #tableData <= 4 then
        candidateList.scrollPane.touchEffect = false;
    else
        candidateList.scrollPane.touchEffect = true;
    end
end

function UIBattlePassTips:ClickOneChoose(v)
    self.chooseItemIndex = v[1];
    local icon = self:GetChild("chooseItems");
    icon:GetController("choose").selectedIndex = 0;
    logicMgr.CostManager.RefreshItem(icon:GetChild("item"), {v[2]});
end

function UIBattlePassTips:OnBuy()
    logicMgr.CostManager.getRewardByIdAndIndex(self.activeId, {}, self.modelTableData.f_ProductId, 1, self.modelTableData.f_Price, self.onBuySuccess, self);
    self:OnClose();
end

function UIBattlePassTips:OnBuyBP()
    local costNum = self.needBpPart * self.bpProportion[1];
    local curNum = logicMgr.ItemManager.getCurrencyCountByModelId(tonumber(1));
    if curNum < costNum then
        UISysTips.AddMsg(string.format(getLanguage("Character_Tips19"),ItemManager.GetItemName(CurrencyEnum.GOLD)));--红魔石不足
        return;
    end
    costHandler.reqCostBuyItem(self.activeId, {self.needBpPart});
    self:OnClose();
end

function UIBattlePassTips:OnSure()
    if self.chooseItemIndex == nil then
        UISysTips.AddMsg("请选择您要的奖励");
    else
        logicMgr.CostManager.chooseItemByCostIdAndIndex(self.activeId, 0, {self.chooseItemIndex});
        self:OnClose();
    end
end


function UIBattlePassTips:onBuySuccess(id, index)
end

-- 关闭
function UIBattlePassTips:OnClose()
    self:OnBack();
end

-- 隐藏
function UIBattlePassTips:OnDisable()
end

-- 卸载
function UIBattlePassTips:OnDestroy()
    if self.RoleLihui then
        destroyImmediate(self.RoleLihui)
        self.RoleLihui = nil
    end
    if self._package and UIPackage.GetById(self._package) then
        UIPackage.RemovePackage(self._package)
    end
end

return UIBattlePassTips